home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-12-02 | 4.7 KB | 201 lines | [TEXT/CWIE] |
- /*
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
- */
-
- #include "ScriptableStuffItEngine.h"
-
- #ifndef __SCRIPT__
- # include <Script.h>
- #endif
-
- #ifndef __PLSTRINGFUNCS__
- # include <PLStringFuncs.h>
- #endif
-
- #ifndef __StuffItLib__
- # include "StuffItEngineLib.h"
- #endif
-
- #ifndef __ERRORS__
- # include <Errors.h>
- #endif
-
- static pascal OSErr GetOptionalFilenameTemplate (const AppleEvent *ae, Str31 nameTemplate)
- {
- OSErr err = noErr;
- DescType actualType;
- Size actualSize;
-
- err = AESizeOfParam (ae,keyNameTemplate,&actualType,&actualSize);
-
- if (err == errAEDescNotFound)
- {
- err = noErr;
- *nameTemplate = 0;
- }
- else if (actualType != typeChar)
- err = paramErr;
- else
- {
- Size largestLegalSizeForNameTemplate = sizeof (Str31) - 1;
-
- if (actualSize > largestLegalSizeForNameTemplate)
- err = paramErr;
- else if (!(err = AEGetParamPtr (ae,keyNameTemplate,typeChar,
- &actualType,nameTemplate+1,largestLegalSizeForNameTemplate,&actualSize)))
- {
- *nameTemplate = actualSize;
- }
- }
-
- return err;
- }
-
- static pascal OSErr FSpIsDir (const FSSpec *spec, Boolean *isDir)
- {
- OSErr err = noErr;
-
- CInfoPBPtr pbp = (CInfoPBPtr) NewPtrClear (sizeof (*pbp));
-
- if (!pbp)
- err = MemError ( );
- else
- {
- pbp->dirInfo.ioVRefNum = spec->vRefNum;
- pbp->dirInfo.ioDrDirID = spec->parID;
- pbp->dirInfo.ioNamePtr = (StringPtr) spec->name;
-
- err = PBGetCatInfoSync (pbp);
-
- if (!err)
- *isDir = (pbp->hFileInfo.ioFlAttrib & ioDirMask) != 0;
-
- DisposePtr ((Ptr) pbp);
- }
-
- return err;
- }
-
- static pascal OSErr GetDestination (const AppleEvent *ae, const FSSpec *sourceFSS, FSSpecPtr destFSS)
- {
- OSErr err = noErr;
-
- Size actualSize;
- DescType actualType;
-
- if (!(err = AEGetParamPtr (ae,keyDestination,typeFSS,&actualType,destFSS,sizeof(*destFSS),&actualSize)))
- {
- if (typeFSS != actualType)
- err = paramErr;
- else
- {
- Boolean isDir;
-
- if (!(err = FSpIsDir (destFSS,&isDir)) && isDir)
- {
- //
- // gotta provide a filename even though the engine will not use it
- //
-
- Str63 buf = "\p:";
- PLstrcat (buf,destFSS->name);
- PLstrcat (buf,"\p:segment");
-
- err = FSMakeFSSpec (destFSS->vRefNum,destFSS->parID,buf,destFSS);
- if (err == fnfErr) err = noErr;
- }
- }
- }
- else if (err == errAEDescNotFound)
- {
- err = FSMakeFSSpec (sourceFSS->vRefNum,sourceFSS->parID,sourceFSS->name,destFSS);
- }
-
- return err;
- }
-
- static pascal OSErr SpoofSourceFileName (const AppleEvent *ae, FSSpecPtr fssP, Str31 oldFileName)
- {
- //
- // This function exists only because StuffIt Engine does not listen when
- // you tell it what segment name to use; it always derives the segment name
- // from the source file name. So we change the source file name. It's up
- // to the caller of this function to change it back.
- //
-
- OSErr err = noErr;
-
- if (*(fssP->name) > 31)
- err = paramErr;
- else
- {
- Str31 filenameTemplate;
-
- if (!(err = GetOptionalFilenameTemplate (ae,filenameTemplate)) && *filenameTemplate)
- {
- if (!(err = FSpRename (fssP,filenameTemplate)))
- {
- PLstrcpy (oldFileName,fssP->name);
- PLstrcpy (fssP->name,filenameTemplate);
- }
- }
- }
-
- return err;
- }
-
- pascal OSErr MakeSegments (const AppleEvent *ae, AppleEvent *reply, long magicCookie)
- {
- AEDesc fssDesc;
-
- OSErr err = AEGetParamDesc (ae,keyDirectObject,typeFSS,&fssDesc);
-
- if (!err)
- {
- long segSize;
- DescType actualType;
- Size actualSize;
-
- if (!(err = AEGetParamPtr
- (ae,keySegmentSize,typeLongInteger,&actualType,&segSize,sizeof(segSize),&actualSize)))
- {
- Boolean selfExtracting = false;
-
- if (!(err = GetOptionalBoolean (ae,&selfExtracting,keySegsAreSelfExtracting)))
- {
- HLockHi (fssDesc.dataHandle);
- FSSpecPtr sourceFSS = FSSpecPtr (*(fssDesc.dataHandle));
-
- FSSpec destFSS;
- short numSegments;
- Str31 oldName;
-
- if (!(err = CountSegments (magicCookie,sourceFSS,segSize,&numSegments)))
- if (!(err = GetDestination (ae,sourceFSS,&destFSS)))
- if (!(err = SpoofSourceFileName (ae,sourceFSS,oldName)))
- {
- if (!(err = SegmentFSSpec (magicCookie,sourceFSS,&destFSS,segSize,selfExtracting,false)))
- err = AEPutParamPtr (reply,keyDirectObject,typeShortInteger,&numSegments,sizeof(numSegments));
-
- if (PLstrcmp (sourceFSS->name,oldName))
- {
- OSErr err2 = FSpRename (sourceFSS,oldName);
- if (!err) err = err2;
- }
- }
- }
- }
-
- OSErr err2 = AEDisposeDesc (&fssDesc);
- if (!err) err = err2;
- }
-
- return err;
- }
-